<?php


define('DISABLEDEFENSE', true);
define('DISABLEXSSCHECK', true);
require '../../../../class/class_core.php';
C::app()->init();

require_once "WxPay.Config.php";
require_once "lib/WxPay.Api.php";
require_once "lib/WxPay.Notify.php";


global $_G;

define('BOAN_PAYMENT_NAME', 'plugin/boan_payment');

if(empty($_G['cache']['plugin'])){
    loadcache('plugin');
}

$vars = $_G['cache']['plugin']['boan_payment'];

class PayNotifyCallBack extends WxPayNotify
{
	//查询订单
	public function Queryorder($transaction_id)
	{   global $_G;
    	if(empty($_G['cache']['plugin'])){
    	    loadcache('plugin');
    	}
	
	    $vars = $_G['cache']['plugin']['boan_payment'];
		$input = new WxPayOrderQuery();
		$input->SetTransaction_id($transaction_id);

		$config = new WxPayConfig($vars);
		$result = WxPayApi::orderQuery($config, $input);
		if(array_key_exists("return_code", $result)
			&& array_key_exists("result_code", $result)
			&& $result["return_code"] == "SUCCESS"
			&& $result["result_code"] == "SUCCESS")
		{
			return true;
		}
		return false;
	}

	/**
	*
	* 回包前的回调方法
	* 业务可以继承该方法，打印日志方便定位
	* @param string $xmlData 返回的xml参数
	*
	**/
	public function LogAfterProcess($xmlData)
	{
        	   
		return;
	}
	
	//重写回调处理函数
	/**
	 * @param WxPayNotifyResults $data 回调解释出的参数
	 * @param WxPayConfigInterface $config
	 * @param string $msg 如果回调处理失败，可以将错误信息输出到该方法
	 * @return true回调出来完成不需要继续回调，false回调处理未完成需要继续回调
	 */
	public function NotifyProcess($objData, $config, &$msg)
	{
		$data = $objData->GetValues();
		
		if(!array_key_exists("return_code", $data) 
			||(array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) {
			
			$msg = lang(BOAN_PAYMENT_NAME, 'wxpay_err_return_code');
			return false;
		}
		if(!array_key_exists("transaction_id", $data)){
		    
		    $msg = lang(BOAN_PAYMENT_NAME, 'wxpay_err_transaction_id');
			return false;
		}

		try {
			$checkResult = $objData->CheckSign($config);
			if($checkResult == false){
			    $msg = lang(BOAN_PAYMENT_NAME, 'wxpay_err_sign');
				return false;
			}
		} catch(Exception $e) {
		}

		
		//查询订单，判断订单真实性
		if(!$this->Queryorder($data["transaction_id"])){
		    $msg = lang(BOAN_PAYMENT_NAME, 'wxpay_err_queryorder');
			
			return false;
		}
		$order = C::t('#boan_payment#boan_payment_order')->fetch($data['out_trade_no']);
		$order_id = $order['order_id'];
		$lockid = 'pay_'.$order_id;
		if(!discuz_process::islocked($lockid)){
		    if($order['status'] == 0){
		        C::m('#boan_payment#payment')->updateOrder($order_id,['pay_method'=>'wxpay','status'=>1,'pay_time'=>strtotime($data['time_end']),'trade_id'=>$data['transaction_id']]);
		        C::m('#boan_payment#payment')->log($data['transaction_id'],2,'wxpay',lang(BOAN_PAYMENT_NAME, 'order_info1'), 2,$data);
		        C::m('#boan_payment#payment')->pluginNotify($order_id,'payNotify');
		    }
		    discuz_process::unlock($lockid);
		}
		return true;
	}
}

$config = new WxPayConfig($vars);
$notify = new PayNotifyCallBack();
$notify->Handle($config, false);
